#include "NetCal.hpp"
#include "Protocol.hpp"
#include "TcpServer.hpp"
#include "Daemon.hpp"

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }

    // std::unique_ptr<Socket> tcpsockp = std::unique_ptr<TcpSocket>();
    // tcpsockp->BuildTcpSocketMethod();
    // std::unique_ptr<Socket> udpsockp = std::unique_ptr<UdpSocket>();
    // udpsockp->BuildUdpSocketMethod();

    std::cout << "服务器已经启动, 已经是一个守护进程了" << std::endl;

    // 守护进程化
    Daemon(0, 0);

    Enable_Console_Log_Strategy();

    // 1. 顶层
    std::unique_ptr<Cal> cal = std::make_unique<Cal>();

    // 2. 协议层
    std::unique_ptr<Protocol> protocolptr = std::make_unique<Protocol>([&cal](Request &req) -> Response
                                                                       { return cal->Execute(req); });

    // 3. 服务器层
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argv[1]), [&protocolptr](std::shared_ptr<Socket> &sock, InetAddr &client)
                                                                  { protocolptr->GetRequest(sock, client); });
    tsvr->Start();

    return 0;
}